package com.itheima.a12.test0;

import com.itheima.a12.test0.A.InvocationHandler;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

/**
 * @ClassName $Proxy0
 * @Description TODO
 * @Author tant
 * @Date 2025/5/21 15:46
 */
public class $Proxy0 implements A.Foo {
    private InvocationHandler h;

    public $Proxy0(InvocationHandler h) {
        this.h = h;
    }

    @Override
    public void foo() {

        try {
//            Method foo = A.Foo.class.getMethod("foo"); //使用静态代码快初始化获取
            h.invoke(this, foo, new Object[0]);
        } catch (Throwable e) {
            e.printStackTrace();
        }


    }

    @Override
    public int bar() {
        try {
//            Method bar = A.Foo.class.getMethod("bar"); //使用静态代码快初始化获取
            return (int) h.invoke(this, bar, new Object[0]);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable e) {
            throw new UndeclaredThrowableException(e);
        }
    }

    static Method foo;
    static Method bar;
    static {
        try {
            foo = A.Foo.class.getMethod("foo");
            bar = A.Foo.class.getMethod("bar");
        } catch (NoSuchMethodException e) {
            throw new NoSuchMethodError(e.getMessage());
        }
    }
}

    